using System;

namespace ChatServer
{
	public class UserManager
	{
		public UserManager ()
		{
		}
		public static int GetIdByName(string name)
		{
			var a = MainClass._d.ExcuteScalar( new object[]{
				"select id from tbluser where username = ?1",
				name
			});
			return (a == null)?-1:(int)a;
		}
		public static string getUserInfo(int id){
			var a = MainClass._d.ReadTable(new object[]{
				"select * from tbluser where id = ?1",
				id
			});
			if(a != null){
				var arrow = a.Rows[0];
				if(arrow==null){return "";}
				return arrow[1].ToString() + "|" + arrow[10].ToString() + "|" + arrow[11].ToString() + "|" + arrow[4].ToString();
			}else{return "";}
		}
		public static void StartServer()
		{
			MainClass._d.Excute( new object[]{
				"update  tbluser set status = 0"
			});
		}
		public static string GetNameByID(int id)
		{
			var a = MainClass._d.ExcuteScalar(new object[]{
				"select username from tbluser where id = ?1",
				id
			});
			return (string)a;
		}
		public static void deletefriend(int uid, string uname)
		{
			int id = GetIdByName(uname);
			var listfriend=MainClass._d.ExcuteScalar( new object[]{
				"select listfriend from tblfriendlist where userid = ?1",
				uid
			});
			if(listfriend != null){
				string lf = listfriend.ToString().Replace("|" + id,"");
				MainClass._d.Excute( new object[]{
					"update tblfriendlist set listfriend = ?1 where userid = ?2",
						lf
						,
						uid
				});
			}
		}
		public static bool adduser(string username, int uid)
		{
			var a=MainClass._d.ExcuteScalar( new object[]{
				"select id from tbluser where username = ?1",
				username
			});
			if(a == null){return false;}
			else{
				if(a.ToString() == uid.ToString()){
					return false;
				}else{
					var listfriend=MainClass._d.ExcuteScalar( new object[]{
						"select listfriend from tblfriendlist where userid = ?1",
						uid
					});
					if(listfriend == null){
						MainClass._d.Excute( new object[]{
							"insert into tblfriendlist(listfriend,userid) values(?1,?2)",
							"|" + a.ToString() 
							,
							uid
						});
					}else{
						if(listfriend.ToString().Contains("|" + a.ToString())){return false;}
						MainClass._d.Excute( new object[]{
							"update tblfriendlist set listfriend = ?1 where userid = ?2",
								listfriend.ToString() + "|" + a.ToString()
								,
								uid
						});
					}
					return true;
				}
			}
		}
		public static string getAvatar(int uid)
		{
			var a=MainClass._d.ExcuteScalar( new object[]{
				"select avatar from tbluser where id = ?1",
				uid
			});
			return (String)a;
		}
		public static string GetFullName(int uid)
		{
			var a=MainClass._d.ExcuteScalar( new object[]{
				"select fullname from tbluser where id = ?1",
				uid
			});
			return (String)a;
		}
		public static string GetFrList(int uid)
		{
			var a=MainClass._d.ExcuteScalar( new object[]{
				"select listfriend from tblfriendlist where userid = ?1",
				uid
			});
			if(a==null){return "";}
			return (String)a;
		}

		public static string GetStatusText(int uid)
		{
			var a=MainClass._d.ExcuteScalar( new object[]{
				"select statustext from tbluser where id = ?1",
				uid
			});
			return (String)a;
		}
		public static string getHash(int uid)
		{
			Object hash = MainClass._d.ExcuteScalar(new object[]{
				"select hash from tbluser where id =?1",
				uid.ToString()
				}
				);
			if (Ullity.Trim(hash) == ""){
				hash = Ullity.GenHash();
				MainClass._d.Excute(new object[]{
					"update tbluser set hash = ?1 where id =?2",
					hash,
					uid
				});
			}
			return hash.ToString();
		}
		public static void SetUserOffline(int uid){
			MainClass._d.ExcuteScalar(new object[]{
				"update tbluser set status=0 where id =?1",
				uid.ToString()
			}
			);
		}
		public static void SetUserOnline(int uid){
			MainClass._d.ExcuteScalar(new object[]{
				"update tbluser set status=1,lastlogin=?2 where id =?1",
				uid.ToString(),
				Environment.TickCount
			}
			);
		}
	}
}

